<?php
function _asUpFiles($dir, $tmp_name, $upfilename, $size = 0, $max_size = '', $type = '', $name = false) {
	if (! file_exists ( $dir ))
		return false;
	if (! is_writable ( $dir )) {
		showmsg ( "上傳圖片失敗：上傳目錄 " . $dir . " 無法寫入!", 0 );
		return false;
	}
	require_once (QISHI_ROOT_PATH . 'include/Iohandler.php');
	$iohandler = new Iohandler();
	$date=date("Y-m-d");
	$iohandler->MakeDir($dir.$date);
	if (! ($upfilename === '')) {
		if (! is_uploaded_file ( $tmp_name )) {
			return false;
		}
		if ($max_size > 0 && $size / 1024 > $max_size) {
			return false;
		}
		$ext_name = strtolower ( str_replace ( ".", "", strrchr ( $upfilename, "." ) ) );
		if (! ($type === '') && strpos ( $type, $ext_name ) === false) {
			showmsg ( "上傳圖片失敗：只允許上傳 " . $type . " 的文件！", 0 );
			exit ();
		}
		($name == true) ? $uploadname = time () . mt_rand ( 100, 999 ) . "." . $ext_name : '';
		($name == false) ? $uploadname = $upfilename : '';
		! is_bool ( $name ) ? ($uploadname = $name . "." . $ext_name) : '';
		//$uploadname = $name ? md5(uniqid(rand())).".".$ext_name : $upfilename; 
		if (! move_uploaded_file ( $tmp_name, $dir. $date. '/'. $uploadname )) {
			return false;
		}
		return array('name' => $upfilename, 'path' => $date. '/'. $uploadname);
	} else {
		return false;
	}
}
?>
